package com.wondersgroup.fjzlpt.org.service.impl.jgqy.jrylt;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.quick.framework.exception.ServiceException;
import com.quick.framework.util.LoggerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqKsxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqYljgDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgxx.YljgxxDAO;
import com.wondersgroup.fjzlpt.org.service.inter.jgqy.jrylt.JryltService;


@Service
public class JryltServiceImpl implements JryltService{

	@Autowired
	private QysqDAO qysqDAO;
	
	@Autowired
	private QysqYljgDAO qysqYljgDAO;
	
	@Autowired
	private YljgxxDAO yljgxxDao;
	
	@Autowired
	private QysqKsxxDAO qysqKsxxDAO;
	
	/**
	 * 保存
	 * @param map
	 * @return
	 */
	public JSONObject save(Map<String, String> map) {
		ResultObject ro = new ResultObject();
		qysq(map, ro);
		return (JSONObject)JSON.toJSON(ro);
	}
	
	/**
	 * 签约申请
	 * @param map
	 * @param ro
	 */
	private void qysq(Map map, ResultObject ro){
		try {
			String yltid = (String) map.get("YLTID");
			String yljgid = (String) map.get("YLJGID");
			map.put("JLZT", "1");
			String ksdms = (String) map.get("KSDMS");
			if("03".equals(map.get("YLTLX")) && !StringUtils.isEmpty(ksdms)){
				String[] ary = ksdms.split(",");
				List<Map> ksList = new ArrayList<Map>();
				for (String str : ary) {
					Map ma = new HashMap<>();
					ma.put("YLTID", yltid);
					ma.put("YLJGID", yljgid);
					ma.put("KSDM", str);
				}
				/**插入专科联盟科室信息*/
				qysqKsxxDAO.insertBatch(ksList);
			}
			map.put("GLID", UUID.randomUUID().toString().toUpperCase());
			map.put("JGLX", "2");
//			map.put("QYSQSJ", (new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
			map.put("QYSQSJ", new Date());
			map.put("QYZT", "02");
			qysqYljgDAO.insert(map);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000511","签约申请失败！");
		}
	}
	
	
	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	//@Cacheable("queryPage")
	public JSONObject queryPage(Map<String, String> map) {
		return qysqDAO.queryPage(map);
	}

	/**
	 * 根据id查询签约申请
	 * @param map
	 * @return
	 */
	public JSONArray queryList(Map<String, String> map) {
		JSONArray jsary = new JSONArray();
		try {
			jsary = qysqDAO.queryAll(map);
			JSONObject json = new JSONObject();
			json.put("YLTMC", "签约名称");
			json.put("ZDYLJGMC", "主导机构");
			json.put("YLTLXMC", "医联体类型");
			json.put("CJSJ", "创建时间");
			json.put("QYYXQMC", "签约有效期");
			json.put("QYZT", "状态");
			jsary.add(0,json);
		} catch (Exception e) {
			LoggerUtil.error(e.getMessage());
			throw new ServiceException("ERR-000512","根据id查询医联体失败！");
		}
		return jsary;
	}

	/**
	 * 查询签约机构信息
	 * @param map
	 * @return
	 */
	public JSONArray queryQyJg(Map<String, String> map) {
		JSONArray ja = new JSONArray();
		if("03".equals(map.get("YLTLX"))){
			ja = qysqDAO.queryQyJg(map);
			JSONArray ary = qysqKsxxDAO.query(map);
			Iterator<Object> it = ja.iterator();
			ja = new JSONArray();
			while (it.hasNext()) {
				JSONArray temp = new JSONArray();
				String ksmc = "";
				String ksdm = "";
				JSONObject json1 = JSONObject.parseObject(it.next().toString());
				Iterator<Object> ksIt = ary.iterator();
				while (ksIt.hasNext()) {
					JSONObject json2 = JSONObject.parseObject(ksIt.next().toString());
					if((json1.getString("PTJGID")).equals(json2.getString("YLJGID"))
							&& (json1.getString("GLID")).equals(json2.getString("GLID"))){
						ksmc += json2.getString("KSMC")+",";
						ksdm += json2.getString("KSDM")+",";
						temp.add(json2);
					}
				}
				if(StringUtils.isEmpty(ksmc)){
					json1.put("KSMC", "");
					json1.put("KSDM", "");
				}else{
					json1.put("KSMC", ksmc.substring(0, ksmc.lastIndexOf(",")));
					json1.put("KSDM", ksdm.substring(0, ksdm.lastIndexOf(",")));
				}
				json1.put("KSXX", temp);
				ja.add(json1);
			}
		}else{
			ja = qysqDAO.queryQyJg(map);
		}
		return ja;
	}

	/**
	 * 获取科室信息 
	 * @param map
	 * @return
	 */
	public JSONObject querZdjgKsxx(Map<String, String> map) {
		JSONObject res = new JSONObject();
		/**获取关联ID*/
		JSONObject jo = qysqYljgDAO.queryZdjg(map);
		map.put("GLID", jo.getString("GLID"));
		JSONArray ja = qysqKsxxDAO.query(map);
		JSONObject temp = new JSONObject();
		String ksmc = "";
		String ksdm = "";
		for (int i = 0; i < ja.size(); i++) {
			temp = (JSONObject) ja.get(0);
			ksmc += temp.getString("KSMC")+",";
			ksdm += temp.getString("KSDM")+",";
		}
		res.put("KSMC", ksmc.substring(0,ksmc.lastIndexOf(",")));
		res.put("KSDM", ksdm.substring(0,ksdm.lastIndexOf(",")));
		return res;
	}


}
